import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);
// vuex 自动持久化工具
// vuex-persistedstate
import createPersistedState from "vuex-persistedstate";
import cartStore from "./module/cartStore";
// vuex vue的公共状态管理
export default new Vuex.Store({
    state: {
        count: 1,
        cname: "你好",
        token: "",
    }, //设计的公共状态
    plugins: [createPersistedState()],
    mutations: {
        changeCount(state, num) {
            state.count += num;
        },
        changeToken(state, token) {
            state.token = token;
            localStorage.token = token;
        },
    }, //可以同步修改state的方法 触发commit
    actions: {
        // countAction(context, num) {
        //     context.commit("changeCount", num);
        // },
        countAction({ commit }) {
            commit("changeCount", num);
        },
    }, //可以异步修改state的方法 触发dispatch
    getters: {
        gcount: (state) => {
            return state.count * 2;
        },
        cname: (state) => state.cname,
        token: (state) => {
            if (localStorage.token) {
                return localStorage.token;
            } else {
                return state.token;
            }
        },
    }, //对state的计算处理 类似计算属性
    modules: {
        cartStore,
    }, //vuex的模块化管理
});